﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Threading;
using System.Web;

using $prjsn$.Tools.IoC;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using ProjectBase.Core;
using ProjectBase.Data.Db;
using ProjectBase.Data.NH;
using ProjectBase.Data.Xml;

using $prjsn$.Common;
using $safeprojectname$.Sessions;

namespace $safeprojectname$
{
    public class TestBase
    {
        public virtual void MyTestInitialize()
        {
            // TODO put common init
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
        }

        public virtual void MyTestCleanup()
        {
            // TODO put common finalize
        }

        public virtual TestContext TestContext { get; set; }
    }

    public class EntityTestBase<T> : TestBase
        where T : class, IFacade
    {
        #region members

        private T _facade;

        #endregion members

        #region Context

        /// <summary>
        /// Creates the System.Web.HttpContext.Current instance
        /// </summary>
        protected virtual void SetContext()
        {
            if (HttpContext.Current.Is())
            {
                return;
            }
            var path = Path.GetFullPath(@"..\..\..\Tests\$safeprojectname$\");
            var request = new HttpRequest(path, "http://localhost/", "");
            var response = new HttpResponse(new StringWriter());
            var context = new HttpContext(request, response);
            HttpContext.Current = context;
        }

        protected virtual void SetPrincipal()
        {
            if (Thread.CurrentPrincipal.Identity.IsAuthenticated)
            {
                return;
            }
            var identity = WindowsIdentity.GetCurrent();
            var principal = new WindowsPrincipal(identity);
            Thread.CurrentPrincipal = principal;
        }

        #endregion Context

        #region Facade & Messages

        /// <summary>
        /// Provides lazy Facade for this entity
        /// </summary>
        protected virtual T Facade
        {
            get
            {
                if (_facade.IsNull())
                {
                    _facade = FacadeFactory.CreateFacade<T>();
                    //_facade.Messages.Clear();
                }
                return _facade;
            }
            set { _facade = value; }
        }

        /// <summary>
        /// Provides lazy Messages collection to be used for Facades creating
        /// </summary>
        protected virtual Messages Messages
        {
            get { return Facade.Messages; }
            set { Facade.Messages = value; }
        }

        #endregion Facade & Messages

        #region Additional test attributes

        public override void MyTestInitialize()
        {
            base.MyTestInitialize();

            Facade = null;
            StructureMapFactoryProvider.ResetDefaults();

            SetContext();

            NHSimpleSession.Initialize();
            NHSession.TransactionBegin();

            DbSimpleSession.Initialize();
            DbSession.TransactionBegin();

            XmlSimpleSession.Initialize();
            XmlSession.TransactionBegin();

            AopFilterManager.Interceptors.Add(() => new Aop.TraceableFilter());

            SetPrincipal();
        }

        public override void MyTestCleanup()
        {
            base.MyTestCleanup();

            NHSession.TransactionRollback();
            NHSession.Dispose();
            DbSession.TransactionRollback();
            XmlSession.TransactionRollback();
        }

        #endregion Additional test attributes
    }
}